﻿using LY.Entity;
using LY.Entity.Entities;
using LY.Entity.PageSearch;
using LY.Entity.SearchEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LY.DataAccess.Repository
{
    public class CompanyRepository : BaseRepository
    {
        //根据会员ID查询公司信息
        public U_Company GetCompany(int memberId)
        {
            string sql = @"SELECT [Id]
                          ,[MemberId]
                          ,[MemberName]
                          ,[MemberTel]
                          ,[CompanyName]
                          ,[Nature]
                          ,[Corporate]
                          ,[CorporateIDCard]
                          ,[CorporateTel]
                          ,[Registercapital]
                          ,[Adress]
                          ,[ManageAdree]
                          ,[EstablishTime]
                          ,[Industry]
                          ,[ICP]
                          ,[UnitTel]
                          ,[Faxes]
                          ,[URL]
                          ,[Email]
                          ,[Postcode]
                          ,[Employees]
                          ,[Synopsis]
                          ,[Constitution]
                          ,[Status]
                          ,[Reason]
                      FROM [U_Company] where MemberId=@MemberId";
            return base.Get<U_Company>(sql, new { MemberId = memberId });
        }

        //查询公司列表
        public PagedList<U_CompanyAnnex_EX> QueryCompanyList(CompanySearch search, PageView view)
        {
            string cols = @"c.[Id]
                              ,c.[MemberId]
                              ,[Name]
                              ,[CompanyName]
                              ,[Registercapital]
                              ,[ManageAdree]
                              ,[Adress]
                              ,[Corporate]
                              ,[CorporateIDCard]
                              ,[MemberName]
                              ,[MemberTel]
                              ,c.[Postcode]
                              ,[Faxes]
                              ,[Employees]
                              ,[Industry]
                              ,[Nature]
                              ,[Synopsis]
                              ,[Constitution]
                              ,c.[Status]";
            string sqlWhere = "and c.Status not in(0,3)";
            if (search.MemberId > 0)
            {
                sqlWhere += "AND c.MemberId=" + search.MemberId;
            }
            if (search.Status > 0)
            {
                sqlWhere += "AND c.Status=" + search.Status;
            }
            if (search.CompanyName != null)
            {
                sqlWhere += "AND CompanyName like '%" + search.CompanyName + "%'";
            }
            return base.PageGet<U_CompanyAnnex_EX>(view, cols, @" [U_Company] c left join U_Member m on c.MemberId=m.ID ", sqlWhere, "c.[Id] ASC", "");
        }

        public U_Company GetModel(int id)
        {
            string sql = @"SELECT [Id]
                          ,[MemberId]
                          ,[MemberName]
                          ,[MemberTel]
                          ,[CompanyName]
                          ,[Nature]
                          ,[Corporate]
                          ,[CorporateIDCard]
                          ,[CorporateTel]
                          ,[Registercapital]
                          ,[Adress]
                          ,[ManageAdree]
                          ,[EstablishTime]
                          ,[Industry]
                          ,[ICP]
                          ,[UnitTel]
                          ,[Faxes]
                          ,[URL]
                          ,[Email]
                          ,[Postcode]
                          ,[Employees]
                          ,[Synopsis]
                          ,[Constitution]
                          ,[Status]
                           FROM [U_Company] where Id=@Id";
            return base.Get<U_Company>(sql, new { Id = id });
        }
        //点击添加公司时，显示的列表
        public PagedList<U_Company_EX> QueryCompany_EXList(CompanySearch search, PageView view)
        {
            string cols = @" c.[Id] as Id,c.[MemberId],c.[CompanyName],[Adress],c.[Postcode],[Faxes],[Employees],[Industry],[Nature],[Synopsis],[Constitution]
                          ,c.[Status],m.SftUserName as SftUserName";
            string sqlWhere = " and c.Status=2 and SftUserName is not null";

            if (!string.IsNullOrEmpty(search.CompanyName))
            {
                sqlWhere += " AND c.[CompanyName] LIKE '%" + search.CompanyName + "%'";
            }
            return base.PageGet<U_Company_EX>(view, cols, @" [U_Company] c left join U_Member m on c.MemberId = m.ID ", sqlWhere, " c.[Id]", "");
        }
    }
}
